// var serverPre = "http://127.0.0.1:9082";
//var serverPre = "http://118.26.173.39/applet";
var serverPre = "";
if (window.location.href.indexOf('localhost') > 0 || window.location.href.indexOf('127.0.0.1') > 0) {
    serverPre = "http://127.0.0.1:8087";
}
var authApiPrefix = serverPre + "/api/auth/v1";
var redirectUrl = authApiPrefix + "/login/cas";
var menuUrl = authApiPrefix + '/menu?platformAlias=APPLET';
var systemName = "applet";

Array.prototype.clone = function () {
    return this.slice(0);
};

String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
};

$.fn.serializeJSONNoNull = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (this.value) {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value);
            } else {
                o[this.name] = this.value;
            }
        }
    });
    return o;
};

/**
 * 是否为空或者未定义
 * @param str
 * @returns {boolean}
 */
var isNull = function (str) {
    return str == '' || str == null || str == undefined;
};

/**
 * 是否不为空或者有定义
 * @param str
 * @returns {boolean}
 */
var isNotNull = function (str) {
    return !isNull(str);
};

var transformTimetoDate = function (time) {
    if (!time){
        return '';
    }
    var date = new Date(time);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D + h + m + s;
};

var defaultCreateTime = function () {
    var date = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D + h + m + s;
};

var getUrlParam = function (paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    }
    else {
        return "";
    }
};

var getCookie = function (c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
};

var delCookie = function (name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
};


//顶级窗体
var getTopWin = function () {
    var p = window.parent;
    while (p != p.window.parent) {
        p = p.window.parent;
    }
    return p;
};

/**
 * 校验登录状态
 * @param result
 */
var checkLogin = function (result) {
    if (result['code'] == 3000) {
        layer.confirm('token不存在或者token已过期，请重新登录', function (index) {
            delCookie("token");
            var topWin = getTopWin();
            var currentUrl = topWin.location.href;
            topWin.location = authApiPrefix + "/login/logout?url=" + currentUrl.split("?")[0];
        });
    }
};

$(function () {

    $("*").click(function () {
        //获取checkbox选中项
        /*if ($(this).prop("checked") == true) {
            $(this).parent().parent().css("background", "#b2dba1");
        } else {
            $(this).parent().parent().css("background", "");
        }*/

        $(".layui-form-checkbox").parent().parent().parent().css("background", "");
        $(".layui-form-checked").parent().parent().parent().css("background", "#b2dba1");
    });
});

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function generateUUID(key) {
    return key + "_" + guid();
}
/**
 * 检查数据是否为空白串
 * @param s
 * @returns {boolean}
 */
function isNotBlank(s) {
    return s != undefined && s != null && (s + "").trim().length > 0
}

function isBlank(s) {
    return !isNotBlank(s);
}

